package com.example.okhttp_demo;



import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private read read;
    TextView responseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendRequest = (Button) findViewById(R.id.send_request);
        responseText = (TextView) findViewById(R.id.response_text);
        sendRequest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

            sendRequestWithOkHttp();

    }

    private void sendRequestWithOkHttp() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                //创建okHttpClient对象
                OkHttpClient mOkHttpClient = new OkHttpClient();
                //构建json表单 发送字段
                MediaType JSON=MediaType.parse("application/json;charset=utf-8");
                HashMap<String,String>map=new HashMap<>();
                map.put("ime","888888888888");
                map.put("is_type","1");
                Gson gson=new Gson();
                String data =gson.toJson(map);
                RequestBody body;
                body =RequestBody.create(JSON,data);
                System.out.println(data);

                //创建一个Request
                Request request = new Request.Builder()
                        .url("http://192.168.1.108/api/answerpolice/index")
                        .post(body)
                        .build();
                //new CALL
                Call call = mOkHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        System.out.println("error");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            //测试返回是否成功
                            String responseData = response.body().string();
                            System.out.println(responseData);
                        }
                    }

                });


                //gson解析
                /**
                 * 有消息头 复杂数据 常规方式
                 */


            }
        }).start();
    }
}